package es.juanc.mail.mio;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.mail.internet.InternetAddress;

public class SmtpMail {
	
	protected static class MessageText {
		private String text;	
		private String charset;		
		private String mimeType;	
		public MessageText( String text ) {
			this( text, null, null );
		}	
		public MessageText( String text, String charset ) {
			this( text, charset, null );
		}
		public MessageText( String text, String charset, String mimeType ) {
			this.text = text;	
			this.charset = charset;
			this.mimeType = mimeType;
		}
		public String getText() {
			return text;
		}
		public String getCharset() {
			return charset;
		}	
		public String getMimeType() {
			return mimeType;
		}		
	}
	
	protected static class AttachementFile {
		private String name;	
		private File file;	
		public AttachementFile( String name, File file ) {
			this.name = name;	
			this.file = file;
		}
		public String getNamet() {
			return name;
		}
		public File getFile() {
			return file;
		}		
	}

	private Map toAddresses = new HashMap();
	private Map ccAddresses = new HashMap();
	private Map bccAddresses = new HashMap();
	private Map replyToAddresses = new HashMap();

	private String fromName = "";
	private String fromAddress = "";
	
	private String subject = "";
	private List messageTexts = new ArrayList();
	private List attachemets = new ArrayList();
	
	public SmtpMail(String fromName, String fromAddress, String subject) {
		this.fromName = fromName;
		this.fromAddress = fromAddress;
		this.subject = subject;
	}

	public void addMessageText(String messageText) {
		this.messageTexts.add( new MessageText(messageText) );
	}

	public void addMessageText(String messageText, String charset, String mimeType) {
		this.messageTexts.add( new MessageText(messageText, charset, mimeType) );
	}

	public void addAttachemets(String name, File file) {
		this.attachemets.add( new AttachementFile(name, file) );
	}

	public void addToAddress(String name, String address) {
		this.toAddresses.put(name, address);
	}

	public void addCcAddress(String name, String address) {
		this.ccAddresses.put(name, address);
	}

	public void addBccAddress(String name, String address) {
		this.bccAddresses.put(name, address);
	}

	public void addReplyToAddress(String name, String address) {
		this.replyToAddresses.put(name, address);
	}
	
	protected InternetAddress getFrom() throws UnsupportedEncodingException {
		return new InternetAddress( this.fromAddress, this.fromName );
	}

	protected String getSubject() {
		return subject;
	}

	protected List getMessageTexts() {
		return this.messageTexts;
	}

	protected List getAttachements() {
		return this.attachemets;
	}

	protected InternetAddress[] getReplyToAddresses() throws UnsupportedEncodingException {
		return this.getAddresses( this.replyToAddresses );
	}

	protected InternetAddress[] getToAddresses() throws UnsupportedEncodingException {
		return this.getAddresses( this.toAddresses );
	}

	protected InternetAddress[] getCcAddresses() throws UnsupportedEncodingException {
		return this.getAddresses( this.ccAddresses );
	}

	protected InternetAddress[] getBccAddresses() throws UnsupportedEncodingException {
		return this.getAddresses( this.bccAddresses );
	}
		
	private InternetAddress[] getAddresses(Map addresses) throws UnsupportedEncodingException {
				
		String name = null;
		String email = null;
		List list = new ArrayList();
		
		for( Iterator i = addresses.keySet().iterator() ; i.hasNext() ; ) {
			name = (String) i.next();
			email = (String) addresses.get( name );
			list.add( new InternetAddress( email, name ) );
		}

		if( list.isEmpty() ) {
			return null;
		}

		InternetAddress all[] = new InternetAddress[list.size()];
		all = (InternetAddress[]) list.toArray(all);
		
		return all;
	}
}